package Lesson19.Notepad;

public class SimpleNotepad implements iNotepad
{
	private final String title;
	private final int pageNumbers;
	private Page[] pages;
	
	SimpleNotepad (final String title, final int pageNumbers)
	{
		if (title != null)
			this.title = title;
		else
		{
			System.out.println("Invalid title format");
			this.title = "";
		}
		
		if (pageNumbers > 0)
		{
			this.pageNumbers = pageNumbers;
		}
		else
		{
			System.out.println("Invalid page numbers format!");
			this.pageNumbers = 10;
		}
		
		this.createNotepad();
	}
	private void createNotepad()
	{
		pages = new Page [pageNumbers];
		for (int i=0; i<pages.length; i++)
		{
			pages[i] = new Page(i+1);
		}
	}
	
	@Override
	public void addText(int pageNumber, String newText) 
	{
		if (validatePage(pageNumber))
			pages[pageNumber-1].addText(newText);
	}
	@Override
	public void replaceTextInPage(int pageNumber, String newText) 
	{
		if(validatePage(pageNumber));
		{
			pages[pageNumber-1].deleteText();
			pages[pageNumber-1].addText(newText);
		}
		
	} 
	@Override
	public void deleteTextInPage(Page selectedPage) 
	{
		if (selectedPage != null)
		{
			for (int i=0; i<pages.length; i++)
			{
				if (selectedPage.equals(pages[i]))
					pages[i].deleteText();
			}
		}
	}
	@Override
	public void viewPages() 
	{
		for (int i=0; i<pages.length; i++)
		{
			if (pages[pageNumbers]!=null)
				pages[pageNumbers].reviewPage();
		}
	}
	private boolean validatePage(int pageNumber)
	{
		return (pageNumber > 0) && (pageNumber <=pages.length);
	}
	
}
